/*
 * Copyright (c) 2006 Consolidated Court Automation Programs
 * All rights reserved.
 */
package example;

import java.net.MalformedURLException;
import java.net.URL;
import java.rmi.RemoteException;
import java.util.Calendar;
import java.util.Date;

import javax.xml.rpc.ServiceException;

import us.wi.state.courts.ccap.wcca2.soap.AttorneyCalendar;
import us.wi.state.courts.ccap.wcca2.soap.AttorneyCalendarReportQuery;
import us.wi.state.courts.ccap.wcca2.soap.CaseInfo;
import us.wi.state.courts.ccap.wcca2.soap.CaseRange;
import us.wi.state.courts.ccap.wcca2.soap.CaseRetrievalQuery;
import us.wi.state.courts.ccap.wcca2.soap.CaseSearchQuery;
import us.wi.state.courts.ccap.wcca2.soap.Charge;
import us.wi.state.courts.ccap.wcca2.soap.CivilCaseDetailsResult;
import us.wi.state.courts.ccap.wcca2.soap.CivilCaseParty;
import us.wi.state.courts.ccap.wcca2.soap.CivilJdgmtDetail;
import us.wi.state.courts.ccap.wcca2.soap.CivilJdgmtParty;
import us.wi.state.courts.ccap.wcca2.soap.CriminalCaseDetailsResult;
import us.wi.state.courts.ccap.wcca2.soap.CvJdgmtParty;
import us.wi.state.courts.ccap.wcca2.soap.Defendant;
import us.wi.state.courts.ccap.wcca2.soap.DocketedCvJdgmt;
import us.wi.state.courts.ccap.wcca2.soap.DocketedCvJdmgtReportQuery;
import us.wi.state.courts.ccap.wcca2.soap.PartyName;
import us.wi.state.courts.ccap.wcca2.soap.WCCA2PortType;
import us.wi.state.courts.ccap.wcca2.soap.WCCA2ServiceLocator;
import us.wi.state.courts.ccap.wcca2.soap.WCCA2ServiceLocator;
import us.wi.state.courts.ccap.wcca2.soap.WCCA2SoapBindingStub;

public class AxisClient
{
	/** Set this to the username provided by CCAP */
	private static final String USER = "exampleuser";

	/** Set this to the password provided by CCAP */
	private static final String PASSWORD = "examplepass1/examplepass2";

	/** The WCCA2 SOAP service location */
	private static final String SERVICE_LOC =
		"http://wcca.wicourts.gov/services/WCCA2";

	public static void main(String[] args)
	{
		try
		{
			WCCA2PortType port = getProxy();

			runCaseSearch1(port);
			runCaseSearch2(port);
			runCaseSearch3(port);
			runAttyCalendarSearch(port);
			runDocketedCvJdgmtReport(port);
			retrieveCriminalCase(port);
			retrieveCivilCase(port);
		}
		catch (MalformedURLException e)
		{
			e.printStackTrace();
		}
		catch (ServiceException e)
		{
			e.printStackTrace();
		}
		catch (RemoteException e)
		{
			e.printStackTrace();
		}
	}

	private static void retrieveCriminalCase(WCCA2PortType service)
		throws RemoteException
	{
		System.out.println(
			"Retrieving criminal case 2001CF000057 from county 44 (Outagamie)");
		CaseRetrievalQuery query = new CaseRetrievalQuery();
		query.setCaseNo("2001CF000057");
		query.setCountyNo("44");

		CriminalCaseDetailsResult result = service.retrieveCriminalCase(query);

		System.out.println("--------");
		System.out.println(
			"Defendant name is " + formatName(result.getDefendant()));
		System.out.println("Charges:");

		Charge[] c = result.getChargeList();

		for (int i = 0; i < c.length; i++)
		{
			System.out.println("    " + c[i].getDescr());
		}

		System.out.println("--------");
	}

	private static void retrieveCivilCase(WCCA2PortType service)
		throws RemoteException
	{
		System.out.println(
			"Retrieving civil case 2001SC000384 from county 44 (Outagamie)");

		CaseRetrievalQuery query = new CaseRetrievalQuery();
		query.setCaseNo("2001SC000384");
		query.setCountyNo("44");

		CivilCaseDetailsResult result = service.retrieveCivilCase(query);

		System.out.println("--------");

		System.out.println("Parties:");

		CivilCaseParty[] parties = result.getCivilCasePartyList();
		for (int i = 0; i < parties.length; i++)
		{
			CivilCaseParty party = parties[i];
			System.out.println(
				"    " + formatName(party) + " - " + party.getPartyTypeDescr());
		}
		System.out.println("");

		System.out.println("Civil Judgments:");
		CivilJdgmtDetail[] jdgmts = result.getCivilJdgmtDetailList();
		for (int i = 0; i < jdgmts.length; i++)
		{
			CivilJdgmtDetail jdgmt = jdgmts[i];
			System.out.println("    Total Amount: " + jdgmt.getTotAmt());
			System.out.println("    Debtors:");
			CivilJdgmtParty[] jdgmtParties = jdgmt.getCivilJdgmtPartyList();
			for (int j = 0; j < jdgmtParties.length; j++)
			{
				CivilJdgmtParty party = jdgmtParties[j];
				if (party.getDescr().equals("Debtor"))
				{
					System.out.println("        " + formatName(party));
				}
			}
			System.out.println("");
		}

		System.out.println("--------");
	}

	private static void runCaseSearch1(WCCA2PortType service)
		throws RemoteException
	{
		System.out.println("Running sample case search 1");

		CaseSearchQuery query = new CaseSearchQuery();
		query.setCountyNo("44");

		CaseRange range = new CaseRange();
		range.setYear("2001");
		range.setCaseType("CF");
		range.setStart("50");
		range.setEnd("60");

		query.setCaseRange(range);

		CaseInfo[] results = service.searchCases(query).getCaseInfoList();

		dumpCaseList(results);
	}

	private static void runCaseSearch2(WCCA2PortType service)
		throws RemoteException
	{
		System.out.println("Running sample case search 2");

		CaseSearchQuery query = new CaseSearchQuery();

		query.setCountyNo("44");
		query.setCaseType("CV");
		query.setFilingDateRange("1");
		query.setFilingDate(getDate(2001, 10, 11));

		dumpCaseList(service.searchCases(query).getCaseInfoList());
	}

	private static void runCaseSearch3(WCCA2PortType service)
		throws RemoteException
	{
		System.out.println("Running sample case search 3");

		CaseSearchQuery query = new CaseSearchQuery();

		PartyName pn = new PartyName();
		pn.setLastName("brant");
		pn.setFirstName("peter");

		query.setPartyName(pn);

		dumpCaseList(service.searchCases(query).getCaseInfoList());
	}

	private static void runAttyCalendarSearch(WCCA2PortType service)
		throws RemoteException
	{
		System.out.println("Running attorney calendar search");

		AttorneyCalendarReportQuery query = new AttorneyCalendarReportQuery();
		query.setAttyType("prosAtty"); // or partyAtty
		query.setDate(getDate(2003, 3, 24));
		query.setRange(new Integer(7));
		query.setAttyNo("1015378");

		AttorneyCalendar[] records =
			service.searchAttyCalendars(query).getAttorneyCalendarList();

		System.out.println("--------");
        if (records == null)
        {
            records = new AttorneyCalendar[0];
        }
		System.out.println("Retrieved " + records.length + " results");
		for (int i = 0; i < records.length; i++)
		{
			AttorneyCalendar record = records[i];
			System.out.println(
				record.getDescr()
					+ " at "
					+ record.getStartTime()
					+ " on "
					+ record.getCalDate());
		}
		System.out.println("--------");
	}

	private static void runDocketedCvJdgmtReport(WCCA2PortType service)
		throws RemoteException
	{
		System.out.println("Running docketed civil judgment report");

		DocketedCvJdmgtReportQuery query = new DocketedCvJdmgtReportQuery();
		query.setSearchBy("fullSatisDate");
		query.setCountyNo("44");
		query.setDate(getDate(2002, 1, 2));
		query.setRange(new Integer(7));

		DocketedCvJdgmt[] results =
			service
				.searchDocketedCvJdgmtReports(query)
				.getDocketedCvJdgmtList();

		System.out.println("--------");
		System.out.println("Retrieved " + results.length + " results");
		for (int i = 0; i < results.length; i++)
		{
			DocketedCvJdgmt record = results[i];
			System.out.println(
				(record.getCaseNo() == null
					? "<Confidential>"
					: record.getCaseNo())
					+ " "
					+ (record.getCaption() == null
						? "<Confidential Case Caption>"
						: record.getCaption()));
			CvJdgmtParty[] parties = record.getCvJdgmtPartyList();
			for (int j = 0; j < parties.length; j++)
			{
				CvJdgmtParty party = parties[j];
				System.out.println(
					"      " + formatName(party) + " - " + party.getDescr());
			}
		}
		System.out.println("--------");
	}

	private static WCCA2PortType getProxy()
		throws MalformedURLException, ServiceException
	{
		WCCA2ServiceLocator locator = new WCCA2ServiceLocator();
		WCCA2PortType result = locator.getWCCA2(new URL(SERVICE_LOC));
		WCCA2SoapBindingStub stub = (WCCA2SoapBindingStub) result;
		stub.setUsername(USER);
		stub.setPassword(PASSWORD);
		return result;
	}

	private static String formatName(Defendant defendant)
	{
		return formatName(
			defendant.getNameL(),
			defendant.getNameM(),
			defendant.getNameF(),
			defendant.getSuffix());
	}

	private static String formatName(CvJdgmtParty party)
	{
		return formatName(
			party.getNameL(),
			party.getNameM(),
			party.getNameF(),
			party.getSuffix());
	}

	private static String formatName(CivilJdgmtParty party)
	{
		return formatName(
			party.getNameL(),
			party.getNameM(),
			party.getNameF(),
			party.getSuffix());
	}

	private static String formatName(CivilCaseParty party)
	{
		return formatName(
			party.getNameL(),
			party.getNameM(),
			party.getNameF(),
			party.getSuffix());
	}

	private static String formatName(
		String nameL,
		String nameM,
		String nameF,
		String suffix)
	{
		StringBuffer builder = new StringBuffer();
		if (nameL == null)
		{
			return "";
		}
		else
		{
			builder.append(nameL);
		}

		if (nameF != null || nameM != null || suffix != null)
		{
			builder.append(", ");
		}

		if (nameF != null)
		{
			builder.append(nameF);
			if (nameM != null || suffix != null)
			{
				builder.append(" ");
			}
		}

		if (nameM != null)
		{
			builder.append(nameM);
			if (suffix != null)
			{
				builder.append(" ");
			}
		}

		if (suffix != null)
		{
			builder.append(suffix);
		}

		return builder.toString();
	}

	/**
	 * Note that month is 1-based, not 0-based
	 */
	private static Date getDate(int year, int month, int day)
	{
		Calendar cal = Calendar.getInstance();
		cal.clear();
		cal.set(Calendar.YEAR, year);
		cal.set(Calendar.MONTH, month - 1);
		cal.set(Calendar.DAY_OF_MONTH, day);
		return cal.getTime();
	}

	private static void dumpCaseList(CaseInfo[] results)
	{
		System.out.println("--------");
		System.out.println("Retrieved " + results.length + " results");
		for (int i = 0; i < results.length; i++)
		{
			CaseInfo record = results[i];
			System.out.println(record.getCaseNo() + " " + record.getCaption());
		}
		System.out.println("--------");
	}
}
